package com.cjj.proxy.jdk;

import com.cjj.proxy.House;
import com.cjj.proxy.Xiaoming;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @ClassName JdkProxy
 * @Date 2021/08/08 15:45:42
 * @Created by ccc-j
 * @email ccc-ju@outlook.com
 */
public class JdkProxy implements InvocationHandler {

    private Object target;

    public JdkProxy(Object target){
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("我是房产中介，开始使用jdk动态代理开始监听你了...");
        Object invoke = method.invoke(target, args);
        System.out.println("我是房产中介，开始使用jdk动态代理结束监听你了...");
        return invoke;
    }

    public static void main(String[] args) {
        Xiaoming xiaoming = new Xiaoming();
        JdkProxy jdkProxy = new JdkProxy(xiaoming);
        // jdk动态代理，通过反射机制生成代理类
        House house = (House) Proxy.newProxyInstance(xiaoming.getClass().getClassLoader(), xiaoming.getClass().getInterfaces(), jdkProxy);
        house.mai();
    }
}
